﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.ObjectModel;

namespace Chat.Server
{
    public sealed class ServerEndPointCollection : ICollection<ServerEndPoint>
    {
        private Dictionary<NickName, ServerEndPoint> items = new Dictionary<NickName,ServerEndPoint>();

        public ServerEndPointCollection()
        {
        }

        public bool TryGetEndPoint(NickName nickName, out ServerEndPoint sep)
        {
            bool result = items.TryGetValue(nickName, out sep);

            return result;
        }

        public void ForEach(
            Action<ServerEndPoint> action,
            ICollection<KeyValuePair<ServerEndPoint,Exception>> failedEndPoints)
        {
            foreach (ServerEndPoint sep in items.Values)
            {
                try
                {
                    action(sep);
                }
                catch (Exception ex)
                {
                    if (failedEndPoints != null)
                        failedEndPoints.Add(
                            new KeyValuePair<ServerEndPoint, Exception>(sep, ex));
                }
            }
        }

        public ICollection<NickName> NickNames
        {
            get
            {
                return items.Keys;
            }
        }

        public void Clear(Action<ServerEndPoint> clearAction)
        {
            foreach (ServerEndPoint sep in items.Values)
            {
                try
                {
                    clearAction(sep);
                }
                catch (Exception)
                {
                }
            }

            items.Clear();
        }

        #region ICollection<ServerEndPoint> Members

        public void Add(ServerEndPoint item)
        {
            items.Add(item.LoginData.User, item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(ServerEndPoint item)
        {
            return items.ContainsKey(item.LoginData.User);
        }

        public void CopyTo(ServerEndPoint[] array, int arrayIndex)
        {
            items.Values.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get
            {
                return items.Count;
            }
        }

        bool ICollection<ServerEndPoint>.IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public bool Remove(ServerEndPoint item)
        {
            return items.Remove(item.LoginData.User);
        }

        #endregion

        #region IEnumerable<ServerEndPoint> Members

        IEnumerator<ServerEndPoint> IEnumerable<ServerEndPoint>.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
